﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:b="clr-namespace:XAMLMagicks.Controls.Button"
    >
    <Style TargetType="{x:Type b:ImageButton}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type b:ImageButton}">
                    <Grid Name="DisplayGrid"
                          >
                        <Image Name="DisplayImage" 
                               HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                               VerticalAlignment="{TemplateBinding VerticalAlignment}"      
                               Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=Image}"
                               />
                        <TextBlock Name="TextOverlay" 
                                   Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=TextOverlayVisibility}" 
                                   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=TextOverlay}" 
                                   FontFamily="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=FontFamily}"
                                   FontSize="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=FontSize}"
                                   FontStretch="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=FontStretch}"
                                   FontStyle="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=FontStyle}"
                                   FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=FontWeight}"                                   
                                   Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=Foreground}"
                                   Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=Background}"
                                   HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=TextOverlayHorizontalAlignment}"
                                   VerticalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=TextOverlayVerticalAlignment}"
                                   />
                    </Grid>
                    <DataTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="DisplayImage" Property="BitmapEffect" 
                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageButton}}, Path=MouseOverEffect}">
                            </Setter>
                            <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type b:ImageButton}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
                            <Setter Property="Opacity" Value="0.50"></Setter>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type b:ImageButton}">
                    <ContentPresenter 
                        Margin="{TemplateBinding Control.Padding}" 
                        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                        VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
                        RecognizesAccessKey="True" 
                        />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type b:ImageRadioButton}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type b:ImageRadioButton}">
                    <Grid Name="DisplayGrid"
                          Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Background}"
                          >
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Image Name="DisplayImage" 
                               Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Image}"
                               MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=ImageMaxWidth}"
                               MaxHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=ImageMaxHeight}"
                               HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=ImageHorizontalAlignment}"
                               VerticalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=ImageVerticalAlignment}"                               
                               />
                        <TextBlock Name="TextOverlay" 
                                   Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=TextOverlayVisibility}" 
                                   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=TextOverlay}" 
                                   FontFamily="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontFamily}"
                                   FontSize="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontSize}"
                                   FontStretch="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontStretch}"
                                   FontStyle="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontStyle}"
                                   FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontWeight}"                                   
                                   Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Foreground}"
                                   Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Background}"
                                   HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=TextOverlayHorizontalAlignment}"
                                   VerticalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=TextOverlayVerticalAlignment}"
                                   />
                        <TextBlock Name="BottomText" Grid.Row="1"
                                   Margin="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=BottomTextMargin}"
                                   Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=BottomTextVisibility}" 
                                   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=BottomText}" 
                                   FontFamily="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontFamily}"
                                   FontSize="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontSize}"
                                   FontStretch="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontStretch}"
                                   FontStyle="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontStyle}"
                                   FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=FontWeight}"                                   
                                   Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Foreground}"
                                   Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=Background}"
                                   HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=BottomTextHorizontalAlignment}"
                                   VerticalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=BottomTextVerticalAlignment}"
                                   />
                    </Grid>
                    <DataTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="DisplayImage" Property="BitmapEffect" 
                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type b:ImageRadioButton}}, Path=MouseOverEffect}">
                            </Setter>
                            <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type b:ImageRadioButton}, AncestorLevel=1}, Path=IsChecked}" Value="False">
                            <Setter TargetName="DisplayImage" Property="Opacity" Value=".67" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type b:ImageRadioButton}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
                            <Setter Property="Opacity" Value="0.50"></Setter>
                        </DataTrigger>                        
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type b:ImageRadioButton}">
                    <ContentPresenter 
                        Margin="{TemplateBinding Control.Padding}" 
                        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
                        VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
                        RecognizesAccessKey="True" 
                        />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!-- Make background explicitly transparent so the whole area is clickable -->
        <Setter Property="Background" Value="Transparent" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Background" 
                        Value="{Binding RelativeSource={RelativeSource Self}, Path=IsCheckedBackground}" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

